package com.itheima.health.common;

import com.itheima.health.service.impl.SetmealServiceImplMP;
import com.itheima.health.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class ClearImgJob {
    // 清理前最小等待时间,放到成员变量位置解耦合
    private static final Long MIN_DELETE_WAIT_TIME = 60 * 1000L;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private SetmealServiceImplMP setmealServiceImplMP;

    @Scheduled(cron = "0 */1 * * * ?")
    public void clearImg() {
        Set<String> difference = redisTemplate.opsForSet().members(RedisConst.SETMEAL_PIC_RESOURCES);
        if (difference != null && difference.size() > 0) {
            for (String s : difference) {
                qiniuUtils.deleteFileFromQiniu(s);
                redisTemplate.opsForSet().remove(RedisConst.SETMEAL_PIC_RESOURCES, s);
            }
        }
    }
}
